import {AbilityModel} from "../../controller/model/AbilityModel";
import Configer from "../../frame/apiModel/config/Configer";
import {FinalKey} from "../../constant/FinalConstant";
import {UnitController} from "../../controller/UnitController";
import {Logger} from "../../frame/apiModel/config/Logger";
import {UnitModel} from "../../frame/apiModel/adapterModel/model3/UnitModel";
import {ItemModel} from "../../frame/apiModel/adapterModel/model3/ItemModel";
import {PlayerModel} from "../../frame/apiModel/adapterModel/model4/PlayerModel";
import {LocModel} from "../../frame/apiModel/base/LocModel";
import {PlayerFactory} from "../../frame/PlayerFactory";
import {DataRouterUtil} from "../../utils/router/DataRouterUtil";
import {CodeUtil} from "../../frame/codeAdapter/CodeUtil";
import {AttributeEnum, UnitType} from "../../frame/dataSource/DataModel";
import {Util} from "../../utils/Util";
import {AbilityUtil} from "./AbilityUtil";
import UnitUtil from "../../utils/UnitUtil";
import {DataRouter} from "../../frame/dataSource/DataRouter";
import {EffectModel} from "../../frame/apiModel/adapterModel/model2/EffectModel";
import {ModelFactory} from "../../frame/apiModel/adapterModel/model5/ModelFactory";
import {TimeControllerOpt} from "../../controller/model/ControllerOpt";
import {TimeController} from "../../controller/TimeController";
import MathUtil from "../../frame/codeAdapter/MathUtil";
import {GlobalEnv} from "../../utils/GlobalEnv";
import {ChangeUnitType} from "../../type/Type";
import {ModelType} from "../../frame/apiModel/adapter/adapterInterface/model5/IModelFactory";

export default class EngulfSkillModel extends AbilityModel {
    abilityId: string = Configer.getFinal(FinalKey.engulfSkill) as string;

    actionFun = (activeUnit: UnitModel, activeItem: ItemModel, targetUnit: UnitModel, targetItem: ItemModel, activePlayer: PlayerModel, targetPlayer: PlayerModel, spellId: string, spellLoc: LocModel)  => {
        let success;
        this.engulfSkill(activeUnit, targetUnit);
        return success;
    }

    engulfSkill(activeUnit: UnitModel, targetUnit: UnitModel) {
        const activeUnitId = activeUnit.strId;

        const player = PlayerFactory.getInstance().getUnitOwner(activeUnit);

        const targetUnitId = targetUnit.strId;
        const targetPlayer = PlayerFactory.getInstance().getUnitOwner(targetUnit);

        if (player === targetPlayer) {
        } else {
            Logger.toastProduct('只能吞噬自己的单位', player)
        }


        let activeFamilyId, targetFamilyId, targetFamilyLevel = 0;
        const activeUnitData = DataRouter.getUnitDataByUnitId(activeUnitId);
        if (activeUnitData) {
            activeFamilyId = activeUnitData.family_tpye;
        }

        const targetUnitData = DataRouter.getUnitDataByUnitId(targetUnitId);
        if (targetUnitData) {
            targetFamilyId = targetUnitData.family_tpye;
            targetFamilyLevel = targetUnitData.family_level;
        }


        if (activeFamilyId === targetFamilyId) {
        } else {
            Logger.toastProduct('只能吞噬同类型的单位', player)
            return;
        }
        const nextUnitId = AbilityUtil.eatAUnit(activeUnit, targetFamilyLevel);
        if (nextUnitId) {
            UnitController.getInstance().changeUnit(activeUnit, nextUnitId, ChangeUnitType.other);
            const ret = UnitController.getInstance().isInPets(targetUnit);
            if (ret) {
                const index = UnitController.getInstance().getPetIndex(targetUnit, player);
                UnitController.getInstance().releasePet(player, index);
            }
            UnitController.getInstance().unitDeadRemove(targetUnit)
        }
    }
}